package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Maintain;
import com.vehicle.pojo.MaintainRecord;
import com.vehicle.service.MaintainRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/maintainRecord")
public class MaintainRecordController extends BaseController {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private MaintainRecordService maintainRecordService;
    @RequestMapping(value = "/maintainRecords")
    @ResponseBody
    public DataGridView MaintainRecordList(MaintainRecord maintainRecord,Integer mid,Integer carid){
        //处理数据
        Maintain maintain = new Maintain();
        maintain.setMid(mid);
        maintainRecord.setMaintains(maintain);
        Car car = new Car();
        car.setCarid(carid);
        maintainRecord.setCars(car);

        logger.info(maintainRecord.getMaintains().getMid());
        return maintainRecordService.findAllMaintainRecord(maintainRecord);
    }
    @ResponseBody
    @RequestMapping(value = "/deleteMaintainRecord")
    public ResultDto deleteMaintainRecord(Integer maintainid){
        ResultDto resultDto = new ResultDto();
        try {
            maintainRecordService.deleteMaintainRecord(maintainid);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/addMaintainRecord")
    public ResultDto addMaintainRecord(MaintainRecord maintainRecord){
        ResultDto resultDto = new ResultDto();
        try {
            maintainRecordService.addMaintainRecord(maintainRecord);
            resultDto.setMsg("增加成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("增加失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/updateMaintainRecord")
    public ResultDto updateMaintain(MaintainRecord maintainRecord){
        ResultDto resultDto = new ResultDto();
        try {
            maintainRecordService.updateMaintainRecord(maintainRecord);
            resultDto.setMsg("修改成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/deleteBatchMaintain")
    public ResultDto deleteBatchMaintainByIds(@RequestParam("maintainIds[]") Integer []maintainIds){
        ResultDto resultDto = new ResultDto();
        try {
            maintainRecordService.deleteBatchByIds(maintainIds);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
}
